require 'View/UIBase/UIBase'

UIOptionalGiftTips = UIBase.New(PanelResNames.UIOptionalGiftTips)

function UIOptionalGiftTips.Init(func, id, subId, flashId, itemIndex, chooseItemList, type, assistId)
    UIOptionalGiftTips.activeId = id;
    UIOptionalGiftTips.subId = subId;
    UIOptionalGiftTips.flashId = flashId;
    UIOptionalGiftTips.chooseingIndex = itemIndex;
    UIOptionalGiftTips.backFunction = func;
    UIOptionalGiftTips.type = type;
    UIOptionalGiftTips.chooseItemList = chooseItemList or {};
    UIOptionalGiftTips.assistId = assistId or 0;  --自选价格挡位
    UIBase.Init(UIOptionalGiftTips);
end

function UIOptionalGiftTips:OnCreate()
    self.candidateList = self:GetChild("candidateList")
    self.inventoryList = self:GetChild("inventoryList")
    self.listShow = self:GetChild("listShow")
    self.progress = self:GetChild("progress")
    self.RCTips = self:GetChild("RCTips")
    self.chooseSureBtn = self:GetChild("chooseSureBtn")
    self:GetChild("tips").text = getLanguage("Storetips1020501");
    self.tipsController = self:GetController("tips")
    self.ctrlItem = CtrlManager.GetCtrl(PanelNames.Item);

    self:RegisterOnClick(self:GetChild("chooseSureBtn"),self.OnChooseSure);
    self:RegisterOnClick(self:GetChild("chooseBtn"),self.OnChoose);
    self:RegisterOnClick(self:GetChild("sureBtn"),self.OnSure);
    self:RegisterOnClick(self:GetChild("bgClose"),self.OnClose);
end

-- 显示
function UIOptionalGiftTips:OnEnable()
    if self.activeId == nil or self.subId == nil then
        self:OnClose();
    else
        self:Refresh();
    end
end

function UIOptionalGiftTips:Refresh()
    if self.flashId then
        self.itemTableData = logicMgr.FlashGiftManager.getGiftTableDataById();
        self.itemNum = self.itemTableData[tostring(self.subId)].f_OptionCount;
    elseif self.type == 2 then
        self.itemNum = 1
    else
        self.severData, self.itemTableData, self.modelTableData = logicMgr.CostManager.getCostData(self.activeId);
        self.itemNum = self.itemTableData[tostring(self.subId)].f_OptionCount;
    end
    self.optionItemTableData = G.dataTable["t_optionitem"];
    --self.chooseItemList = {};
    self:ClickChoose(self.chooseingIndex or 1, true);
end

function UIOptionalGiftTips:ClickChoose(itemIndex, type)
    self.tipsController.selectedIndex = 0;
    self:ClickOneChooseChecked(self.chooseingIndex, false);
    if type then
        self.chooseingIndex = nil;
        self.candidateList:RemoveChildrenToPool();
        for i = 1, self.itemNum do
            --self.chooseItemList[i] = {};
            local item = self.candidateList:AddItemFromPool();
            self:UpdataItem(item , i);
            self:ClickOneChooseChecked(i, false);
        end
        self:UpdataProgress();
    end
    self.chooseingIndex = itemIndex;
    if self.chooseingIndex < self.candidateList.numChildren then
        self.chooseSureBtn.title = getLanguage("storedesc1020501");
    else
        self.chooseSureBtn.title = getLanguage("Btn_Confirm");
    end
    self:ClickOneChooseChecked(self.chooseingIndex, true);
    local itemData = self.chooseItemList[self.chooseingIndex]
    self.inventoryList:RemoveChildrenToPool();
    for k, v in pairs(self:findChooseItemsByIdAndIndex(self.subId, itemIndex)) do
        local item = self.inventoryList:AddItemFromPool();
        self.lockData = nil;
        self.isItemLock = false;
        self.isChoose = 0;
        if not logicMgr.CostManager.itemIsOpen(v[1]) then
            self.isItemLock = true;
        end
        --if itemData and itemData[1] == v[1] then
        --    self.isChoose = 1;
        --end
        logicMgr.CostManager.RefreshItem(item, v, {isLock=self.isItemLock, isShowName=true, mailGet=self.isChoose});
        item.onClick:Clear();
        item.onTouchBegin:Clear();
        item.onTouchMove:Clear();
        item.onTouchEnd:Clear();
        --go.onTouchMove:Clear();
        if self.isItemLock then
            item.onClick:Add(function ()
                logicMgr.CostManager.itemIsOpen(v[1], true);
            end);
        else
            item.onTouchBegin:Add(function(evt)
                self:OnTouchStart(evt, v);
            end)
            item.onTouchMove:Add(function(evt)
                self:OnTouchMove(evt);
            end)
            item.onTouchEnd:Add(function()
                self:OnTouchUp(itemIndex, v);
            end)
        end
    end
end
function UIOptionalGiftTips:OnTouchStart(evt, v)
    self.lastEventX = evt.inputEvent.x;
    self.lastEventY = evt.inputEvent.y;
    self.isTouching = true;
    self.timerId = utimer.timeCall(function()
        self.timerId = nil;
        if logicMgr.ItemManager.IsEquip(v[1]) then
            showUI(PanelNames.UIEquipInfo, nil, logicMgr.CostManager.getItemOrEquipDataByIdAndNum(v[1], v[2]));
        else
            showUI(PanelNames.UIBagItemInfo, logicMgr.CostManager.getItemOrEquipDataByIdAndNum(v[1], v[2]));
        end
    end, 500, 1);
end

function UIOptionalGiftTips:OnTouchMove(evt)
    if self.timerId ~= nil then
        if math.abs(self.lastEventY - evt.inputEvent.y) > 5 or math.abs(self.lastEventX - evt.inputEvent.x) > 5 then
            utimer.remove(self.timerId);
            self.timerId = nil;
            self.isTouching = false;
        end
    end
end

function UIOptionalGiftTips:OnTouchUp(itemIndex, v)
    if self.timerId ~= nil and self.isTouching then
        utimer.remove(self.timerId);
        self:ClickOneChoose(itemIndex, v);
        self.timerId = nil;
    end
end

function UIOptionalGiftTips:UpdataItem(itemObj, itemIndex)
    local flag = 0;
    if itemObj == nil then
        return flag;
    end
    local choose = itemObj:GetController("choose");
    local item = itemObj:GetChild("item");
    local itemData = self.chooseItemList[itemIndex];
    if itemData == nil or #itemData == 0 then
        choose.selectedIndex = 0;
        flag = 1;
    else
        choose.selectedIndex = 1;
        logicMgr.CostManager.RefreshItem(item, itemData);
        item.onClick:Clear();
    end
    itemObj.onClick:Clear();
    itemObj.onClick:Add(function() self:ClickChoose(itemIndex, false) end);
    return flag;
end

function UIOptionalGiftTips:ClickOneChooseChecked(itemIndex, state)
    if itemIndex ~= nil and self.candidateList.numChildren >= itemIndex then
        local itemObj = self.candidateList:GetChildAt(itemIndex - 1);
        itemObj:GetChild("checked").visible = state;
        if self.chooseItemList[itemIndex] == nil or #self.chooseItemList[itemIndex] == 0 then
            self.RCTips.text = "请选择你需要的奖励";
        else
            self.RCTips.text = logicMgr.ItemManager.GetItemDesc(self.chooseItemList[itemIndex][1]);
        end
    end
end

function UIOptionalGiftTips:ClickOneChoose(itemIndex, item)
    self.chooseItemList[itemIndex] = item;
    self:UpdataItem(self.candidateList:GetChildAt(itemIndex - 1), itemIndex);
    if self.chooseItemList[itemIndex] == nil or #self.chooseItemList[itemIndex] == 0 then
        self.RCTips.text = "请选择你需要的奖励";
    else
        self.RCTips.text = logicMgr.ItemManager.GetItemDesc(self.chooseItemList[itemIndex][1]);
    end
    self:UpdataProgress();
end

function UIOptionalGiftTips:UpdataProgress()
    local num = 0;
    for k, v in pairs(self.chooseItemList) do
        if next(v) ~= nil then
            num = num + 1;
        end
    end
    self.progress.text = num.."/"..#self.chooseItemList;
end
function UIOptionalGiftTips:ShowTips()
    self.tipsController.selectedIndex = 1;
    self.listShow:RemoveChildrenToPool();
    for k, v in pairs(self.chooseItemList) do
        local item = self.listShow:AddItemFromPool();
        logicMgr.CostManager.RefreshItem(item, v);
    end
end

function UIOptionalGiftTips:OnChooseSure()
    if self.chooseingIndex < self.candidateList.numChildren then
        self:ClickChoose(self.chooseingIndex + 1, false)
        return ;
    end
    local flag = true;
    for k, v in pairs(self.chooseItemList) do
        if #v == 0 then
            flag = false;
            break;
        end
    end
    if flag then
        self:ShowTips();
    else
        UISysTips.AddMsg("请选择您要的奖励");
    end
end

function UIOptionalGiftTips:OnChoose()
    self:ClickChoose(1, false);
end

function UIOptionalGiftTips:OnSure()
    local data = {};
    for k, v in pairs(self.chooseItemList) do
        table.insert(data, v[1]);
    end
    if self.type == 2 then
        local cfgData, serverData = logicMgr.ActiveManager.getActiveData(self.activeId)
        if serverData.totalCount >= cfgData.mustGetSpecialCount then
            UISysTips.AddMsg(getLanguage("ActivityTips2100104")) --大奖已获得
        else
            local boxIdx = logicMgr.ActiveManager.getCelebrationBigAwardIdx(data[1])
            if boxIdx then
                ActivityHandler.ReqShengDanManYueSelectBigRewardMessage(self.activeId, boxIdx)
            end
        end
    else
        logicMgr.CostManager.chooseItemByCostIdAndIndex(self.activeId, self.flashId or self.subId, data, self.assistId);
    end
    if self.backFunction ~= nil then
        self.backFunction(self.chooseItemList);
    end
    self:OnClose()
end

function UIOptionalGiftTips:findChooseItemsByIdAndIndex(id, index)
    for k, v in pairs(self.optionItemTableData) do
        if v.f_BelongActiveId == tonumber(id) and v.f_OptionOrder == index then
            return decodeJsonStrToTabel(v.f_OptionItem);
        end
    end
    return {};
end

-- 关闭
function UIOptionalGiftTips:OnClose()
    self:OnBack();
end

-- 隐藏
function UIOptionalGiftTips:OnDisable()
end

-- 卸载
function UIOptionalGiftTips:OnDestroy()
end

return UIOptionalGiftTips